<?php
defined('IN_XLP') or exit('Access Denied');
$Document = array(
    'pageid'   => 'task-add', //页面标示
    'pagename' => '添加任务', //当前页面名称
    'mycss'    => array('../jquery-ui/css/smoothness/jquery-ui-1.10.0.custom.min'), //加载的css样式表
    'myjs'     => array(), //加载的js脚本
    'footerjs' => array(
		'../ueditor/editor_config',
		'../ueditor/editor.min',
		'../jquery-ui/js/jquery-ui-1.10.0.custom.min',
		'../jquery-ui/js/jquery-ui-timepicker-addon',
	),
	'head'	   => true, //加载头部文件
);
include getTpl('header', 'public');
?>
<style>
label{cursor:pointer;font-weight:normal}
.red{color:red;}
.pt7{padding-top:7px;}
.w70{width:70px;display:inline;}
.w100{width:100px;display:inline;}
#reward_name_input{display:none;}
#ui-datepicker-div{z-index:10000 !important;}
</style>
<!--顶部导航 开始-->
<?php include getTpl('top', 'public');?>
<!--顶部导航 结束-->
<!--左侧菜单 开始-->
<?php include getTpl('nav', 'public');?>
<!--左侧菜单 结束-->
<!--主体 开始-->
<!-- content -->
<section id="content">
	<section class="main padder">
		<div class="clearfix">
			<h4><i class="fa fa-edit"></i>新增任务</h4>
		</div>
		<div class="row">
			<div class="col-sm-12">
				<section class="panel">
					<div class="panel-body">
						<form class="form-horizontal" id="taskAddForm" method="post" autocomplete="off">
                            
                            <div class="form-group">
                                <label class="col-sm-1 control-label">任务标题</label>
                                <div class="col-sm-2">
                                    <input type="text" name="task_title" id="task_title" placeholder="最好少于8个字" class="required form-control" />
                                </div>
                            </div>

							<div class="form-group">
								<label class="col-sm-1 control-label">任务简介</label>
								<div class="col-sm-3">
									<textarea name="task_brief" id="task_brief" cols="30" rows="4" placeholder="200字以内" class="required bg-focus form-control"></textarea>
								</div>
							</div>
							<div class="form-group">
								<label class="col-sm-1 control-label">任务详情</label>
								<div class="col-sm-2">
									<script type="text/plain" style="height:auto;" id="editor"></script>
								</div>
							</div>
                            <div class="form-group">
                                <label class="col-sm-1 control-label">任务时限</label>
                                <div class="col-sm-3">
                                    <input type="text" name="task_day" id="task_day" placeholder="天" class="w70 form-control" />&emsp;天&emsp;
                                    <input type="text" name="task_hour" id="task_hour" placeholder="小时" class="w70 form-control" />&emsp;小时&emsp;
                                    <input type="text" name="task_minute" id="task_minute" placeholder="分钟" class="w70 form-control" />&emsp;分钟
                                    <span class="help-block">超过该时限未完成则任务失败，若无时间限制则不填</span>
                                </div>
                            </div>
							<div class="form-group">
                                <label class="col-sm-1 control-label">起始时间</label>
                                <div class="col-sm-3">
                                    <input class="input-sm required" style="display:inline" name="time_start" id="time_start" size="18" type="text" >
                                    <span>&emsp;&#8764&emsp;</span>
                                    <input class="input-sm" style="display:inline" name="time_end" id="time_end" size="18" type="text" >
                                </div>
                            </div>

							<br><br>
							<div class="form-group">
								<label class="col-sm-1 control-label">奖品类型</label>
								<div class="col-sm-3">
									<select class="form-control" style="width:auto;" name="reward_type" id="reward_type">
										<!-- <option value="0">选择类型</option> -->
										<?php foreach ($reward as $v): ?>
										<option value="<?php echo $v['id'] ?>"><?php echo $v['reward_name'] ?></option>
										<?php endforeach ?>
										<option value="-1">新增其它</option>
									</select>
								</div>
							</div>
							<div class="form-group" id="reward_name_input">
								<label class="col-sm-1 control-label">奖品名称</label>
								<div class="col-sm-2">
									<input type="text" name="reward_name" id="reward_name" placeholder="" class="bg-focus form-control" />
								</div>
							</div>
							<div class="form-group">
								<label class="col-sm-1 control-label">奖品总数</label>
								<div class="col-sm-1">
									<input type="number" min="1" name="reward_total" id="reward_total" placeholder="" class="required form-control" />
								</div>
							</div>
							<div class="form-group">
								<label class="col-sm-1 control-label">每日上限</label>
								<div class="col-sm-1">
									<input type="number" min="1" name="reward_limit" id="reward_limit" placeholder="" class="form-control" />
									<span class="help-block" style="white-space:nowrap">*选填，每人每天获得奖励上限</span>
								</div>
							</div>
							<div class="form-group">
								<label class="col-sm-1 control-label">获奖阈值</label>
								<div class="col-sm-1">
									<input type="number" min="0" name="reward_threshold" id="reward_threshold" placeholder="适用于实物" class="form-control" />
									<span class="help-block" style="white-space:nowrap">*选填，推广次数超过该值，奖品总数才会减少（但不代表用户已获得奖品，该值必须小于奖品汇率的推广次数）</span>
								</div>
							</div>
							<div class="form-group">
								<label class="col-sm-1 control-label" style="padding-top:18px">奖品汇率</label>
								<div class="col-sm-2">
									<input type="number" min="1" name="rate_reward" id="rate_reward" placeholder="奖品数量" class="required w100 form-control" />
									<span style="font-size:36px;vertical-align:-6px;">&#8644</span>
									<input type="number" min="1" name="rate_promotion" id="rate_promotion" placeholder="推广次数" class="required w100 form-control" />
									<span class="help-block">奖品数量 : 推广次数</span>
								</div>
							</div>

							<div class="form-group">
								<label class="col-sm-1 control-label">是否隐藏</label>
								<div class="col-sm-2 pt7">
									<input id="show" type="radio" name="is_show" value="1" checked />
									<label for="show">显&emsp;示</label>&emsp;&emsp;
									<input id="hide" type="radio" name="is_show" value="0" />
									<label for="hide">隐&emsp;藏</label>
								</div>
							</div>

							<div class="form-group pt7">
								<div class="col-sm-9 col-lg-offset-1">
									<div class="col-sm-1">
										<textarea name="task_content" id="task_content" style="display:none;"></textarea>
										<input type="hidden" name="poster_uid" value="<?php echo $poster_uid ?>">
										<button type="submit" id="sub-ok" data-loading-text="正在提交..." class="btn btn-primary">保存</button>
									</div>
									<button type="button" class="btn btn-white">取消</button>
								</div>
							</div>

						</form>
					</div>
				</section>
			</div>
		</div>
	</section>
</section>
<!--/ content --> 
<!--主体 结束-->
<script>
var fixedImagePath   = '';
var uploadUrl		= "<?php echo U('task/upload');?>";
var UEDITOR_HOME_URL = '<?php echo LOCAL_PUBLIC_PATH; ?>ueditor/';

$(function(){

	var msg_editor = new UE.ui.Editor({initialFrameWidth: 690, initialFrameHeight:320});
	msg_editor.render("editor");

	$('#taskAddForm').submit(function(){
		var _info = '';
		$('.required').each(function(){
			if( $.trim($(this).val()) == '' ) {
				var _name = $(this).attr('name');
				switch(_name) {
					case 'task_title':
						_info = '任务标题不能为空';
						break;
					case 'task_brief':
						_info = '任务简介不能为空';
						break;
					case 'reward_total':
						_info = '奖品总数不能为空';
						break;
					case 'rate_reward':
					case 'rate_promotion':
						_info = '奖品汇率不能为空';
						break;
                    case 'time_start':
                        _info = '开始时间不能为空';
                        break;
					case '':
					default:
						break;
				}
				return false;
			}
		});
		if(_info !== '') {
			Msg.error(_info);
			resetSubmit("#sub-ok", '提交');
			return false;
		}
		//任务详情
		var task_content = msg_editor.getContent();
        $('#task_content').val(task_content);
		if($.trim(task_content) == '') {
			Msg.error('任务详情不能为空');
			resetSubmit("#sub-ok", '提交');
			return false;   
		}
		//奖品类型为其它
		if($('#reward_type').val() == "-1" && $('#reward_name').val() == '') {
			Msg.error('奖品名称不能为空');
			resetSubmit("#sub-ok", '提交');
			return false;
		}
		//奖品减少阈值
		var rate_promotion   = parseInt( $('#rate_promotion').val() );
		var reward_threshold = parseInt( $('#reward_threshold').val() );
		if(reward_threshold != NaN && reward_threshold >= rate_promotion) {
			Msg.error('获奖阈值必须小于推广次数');
			resetSubmit("#sub-ok", '提交');
			return false;
		}

		Msg.loading();
		$.post('<?php echo U("task/add"); ?>', $(this).serialize(), function(result){
			Msg.hide();
			resetSubmit("#sub-ok",'提交');
			if(result.status == 1) {
				Msg.ok(result.info);
			} else {
				Msg.error(result.info);
			}
		}, 'JSON');

		return false;
	});

	//时间插件
	$('#time_start').datetimepicker();
	$('#time_end').datetimepicker();

	//奖品类型
	$('#reward_type').change(function(){
		var type = parseInt($(this).val());
		var $reward = $('#reward_name');
		var $reward_name_input = $('#reward_name_input');
		if(type == -1) {
			$reward_name_input.show();
			$reward.focus();
		} else {
			$reward_name_input.hide();
		}
	});
});
</script>
<?php include getTpl('footer', 'public'); ?>